package com.qys.livesMall.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.mapper.FinanceShopAccountDetailMapper;
import com.qys.livesMall.finance.model.dto.shop.EveryDayIncome;
import com.qys.livesMall.shop.mapper.H2ShopRechargeWithdrawalMapper;
import com.qys.livesMall.shop.mapper.ShopOrderSkuMapper;
import com.qys.livesMall.shop.modal.dto.shop.rw.EveryDayData;
import com.qys.livesMall.shop.modal.entity.ShopStatisticsEveryDay;
import com.qys.livesMall.shop.mapper.ShopStatisticsEverydayMapper;
import com.qys.livesMall.shop.service.ShopOrderSkuService;
import com.qys.livesMall.shop.service.ShopStatisticsEverydayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 店铺每日统计 服务实现类
 * </p>
 *
 * @author
 * @since 2023-11-06
 */
@Service
public class ShopStatisticsEverydayServiceImpl extends ServiceImpl<ShopStatisticsEverydayMapper, ShopStatisticsEveryDay> implements ShopStatisticsEverydayService {

    @Autowired
    private ShopOrderSkuMapper shopOrderSkuMapper;

    @Autowired
    private H2ShopRechargeWithdrawalMapper rechargeWithdrawalMapper;
    @Autowired
    private FinanceShopAccountDetailMapper financeShopAccountDetailMapper;
    @Override
    public ShopStatisticsEveryDay getField(Long shopId, LocalDate date, SFunction<ShopStatisticsEveryDay, Object>... functions) {
        LambdaQueryWrapper<ShopStatisticsEveryDay> wq= Wrappers.lambdaQuery(ShopStatisticsEveryDay.class)
                .select(functions)
                .eq(ShopStatisticsEveryDay::getShopId, shopId)
                .eq(ShopStatisticsEveryDay::getDataDate,date)
                .last("limit 1");
        ShopStatisticsEveryDay info =this.getOne(wq);
        return info;
    }

    @Override
    public List<ShopStatisticsEveryDay> getField(Long shopId, SFunction<ShopStatisticsEveryDay, Object>... functions) {
        LambdaQueryWrapper<ShopStatisticsEveryDay> wq= Wrappers.lambdaQuery(ShopStatisticsEveryDay.class)
                .select(functions)
                .eq(ShopStatisticsEveryDay::getShopId, shopId);
        return list(wq);
    }

    @Override
    public List<ShopStatisticsEveryDay> getAllField(Long shopId) {
        LambdaQueryWrapper<ShopStatisticsEveryDay> wq= Wrappers.lambdaQuery(ShopStatisticsEveryDay.class)
                .eq(ShopStatisticsEveryDay::getShopId, shopId);
        return list(wq);
    }

    @Override
    public List<ShopStatisticsEveryDay> getInfoByDate(List<Long> shopIds, LocalDate date) {
        LambdaQueryWrapper<ShopStatisticsEveryDay> wq= Wrappers.lambdaQuery(ShopStatisticsEveryDay.class)
                .in(ShopStatisticsEveryDay::getShopId, shopIds)
                .eq(ShopStatisticsEveryDay::getDataDate,date);
        return list(wq);
    }

    @Override
    public void initData(Long shopId) {
        //店铺订单
        List<ShopStatisticsEveryDay> everyDays= shopOrderSkuMapper.getEveryDayOrderNum(shopId);
        //充值和提现
        List<EveryDayData> rws=rechargeWithdrawalMapper.getEveryDayRwData(shopId);
        //收益
        List<EveryDayIncome> incomes= financeShopAccountDetailMapper.getEveryDayIncome(shopId);

        Map<LocalDate,ShopStatisticsEveryDay> map=new HashMap<>();
        if (CollectionUtils.isNotEmpty(everyDays)){
            for (ShopStatisticsEveryDay everyDay:everyDays){
                everyDay.setShopId(shopId);
                map.put(everyDay.getDataDate(),everyDay);
            }
        }
        if (CollectionUtils.isNotEmpty(rws)){
            for(EveryDayData everyDayData:rws){
                ShopStatisticsEveryDay everyDay=map.get(everyDayData.getOrderDate());
                if (everyDay==null){
                    everyDay=new ShopStatisticsEveryDay();
                    everyDay.setShopId(shopId);
                    everyDay.setDataDate(everyDayData.getOrderDate());
                    map.put(everyDayData.getOrderDate(),everyDay);
                }
                if (Objects.equals(AccountBusinessTypeEnum.SHOP_RECHARGE.getType(),everyDayData.getType())){
                    everyDay.setReAmount(everyDayData.getAmount());
                    everyDay.setReNum(everyDayData.getNum());
                }
                if (Objects.equals(AccountBusinessTypeEnum.SHOP_WITHDRAWAL.getType(),everyDayData.getType())){
                    everyDay.setWlAmount(everyDayData.getAmount());
                    everyDay.setWlNum(everyDayData.getNum());
                }
            }
        }
        if (CollectionUtils.isNotEmpty(incomes)){
            for (EveryDayIncome income:incomes){
                ShopStatisticsEveryDay everyDay=map.get(income.getPayDate());
                if (everyDay==null){
                    everyDay=new ShopStatisticsEveryDay();
                    everyDay.setShopId(shopId);
                    everyDay.setDataDate(income.getPayDate());
                    map.put(everyDay.getDataDate(),everyDay);
                }
                everyDay.setIncome(income.getAmount());
            }
        }
        List<ShopStatisticsEveryDay> upList=map.values().stream().toList();
        if (CollectionUtils.isEmpty(upList)){
            return;
        }
        List<LocalDate> dates=new ArrayList<>();
        upList.stream().forEach(day->{
            if (day.getReAmount()==null){
                day.setReAmount(BigDecimal.ZERO);
            }
            if (day.getWlAmount()==null){
                day.setWlAmount(BigDecimal.ZERO);
            }
            day.setDifference(day.getReAmount().subtract(day.getWlAmount()));
            dates.add(day.getDataDate());
        });
        //删除日期不存在的记录
        LambdaQueryWrapper<ShopStatisticsEveryDay> delWq=new LambdaQueryWrapper();
        delWq.eq(ShopStatisticsEveryDay::getShopId,shopId);
        delWq.notIn(ShopStatisticsEveryDay::getDataDate,dates);
        remove(delWq);
        //获取存在的记录
        LambdaQueryWrapper<ShopStatisticsEveryDay> wq=new LambdaQueryWrapper();
        wq.eq(ShopStatisticsEveryDay::getShopId,shopId);
        List<ShopStatisticsEveryDay> olds=list(wq);
        if (CollectionUtils.isEmpty(olds)){
            saveBatch(upList);
            return;
        }
        Map<LocalDate,Long> oldMap=olds.stream().collect(Collectors.toMap(ShopStatisticsEveryDay::getDataDate,ShopStatisticsEveryDay::getId,(v1,v2)->v1));
        upList.stream().forEach(dayData ->{
            dayData.setId(oldMap.get(dayData.getDataDate()));
        } );
        saveOrUpdateBatch(upList);
    }

    @Override
    public List<ShopStatisticsEveryDay> getYesterday(Integer userSysType) {
        return baseMapper.getYesterday(userSysType);
    }


}
